package com.base.cn.platform.os.service.classes.topic.report;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.dao.classes.topic.report.ClassesTopicReportDao;
import com.base.cn.platform.os.entity.classes.topic.ClassesTopic;
import com.base.cn.platform.os.entity.classes.topic.report.ClassesTopicReport;
import com.base.cn.platform.os.service.classes.topic.ClassesTopicBiz;
import com.base.cn.platform.os.service.manage.user.user.CusUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 班级话题举报
 *
 * @author SK
 * @since 2018-06-04
 */
@Service
public class ClassesTopicReportBiz extends BaseBiz<ClassesTopicReport, ClassesTopicReportDao> {

    @Autowired
    private CusUserService cusUserService;
    @Autowired
    private ClassesTopicBiz classesTopicBiz;

    /**
     * 查询班级话题举报列表
     *
     * @param condition 班级话题举报查询条件
     * @param count     条数
     * @return 班级话题举报列表
     */
    public List<ClassesTopicReport> findClassesTopicReportList(ClassesTopicReport condition,
                                                               Integer count) {
        String where = this.where(condition);
        List<ClassesTopicReport> classesTopicReportList = find(where, count, null);
        this.setClassesTopicReportCreator(classesTopicReportList);
        this.setClassesTopicReportTopic(classesTopicReportList);
        return classesTopicReportList;
    }

    /**
     * 查询班级话题举报列表
     *
     * @param condition  班级话题举报查询条件
     * @param pagination 分页
     * @return 班级话题举报列表
     */
    public PageInfo<ClassesTopicReport> findClassesTopicReportPage(ClassesTopicReport condition,
                                                                   Pagination pagination) {
        if (Objects.nonNull(condition.getUser()) &&
                StringUtils.isNotEmpty(condition.getUser().get("keyWord"))) {
            List<Map<String, Object>> cusUserList = cusUserService.queryUserList(condition.getUser(), false, false, false, false, false, false, false);
            if (ObjectUtils.isEmpty(cusUserList)) {
                return new PageInfo<>();
            }
            String userIds = DataUtil.joining(cusUserList, (v) -> v.get("id"));
            condition.setUserIds(userIds);
        }
        if (Objects.nonNull(condition.getClassesTopic()) &&
                (StringUtils.isNotEmpty(condition.getClassesTopic().getTitle()) ||
                        StringUtils.isNotEmpty(condition.getClassesTopic().getContent()))) {
            List<ClassesTopic> classesTopicList = classesTopicBiz.findClassesTopicList(condition.getClassesTopic(), null, 0, false, false, false);
            if (ObjectUtils.isEmpty(classesTopicList)) {
                return new PageInfo<>();
            }
            String topicIds = DataUtil.joining(classesTopicList, ClassesTopic::getId);
            condition.setTopicIds(topicIds);
        }
        String where = this.where(condition);
        PageInfo<ClassesTopicReport> pageInfo = findPage(where, pagination, null);
        this.setClassesTopicReportCreator(pageInfo.getList());
        this.setClassesTopicReportTopic(pageInfo.getList());
        return pageInfo;
    }

    /**
     * 更新话题举报状态
     *
     * @param ids    话题举报id
     * @param status 1.正常 2.删除
     */
    public void updateClassesTopicReportStatus(String ids, Integer status) {
        ClassesTopicReport condition = new ClassesTopicReport();
        condition.setStatus(status);
        String where = " id in (" + ids + ")";
        updateByWhereSql(condition, where);
    }

    /**
     * 设置班级话题举报的话题
     *
     * @param classesTopicReportList 班级话题举报
     */
    private void setClassesTopicReportTopic(List<ClassesTopicReport> classesTopicReportList) {
        if (ObjectUtils.isNotEmpty(classesTopicReportList)) {
            String topicIds = DataUtil.joining(classesTopicReportList, ClassesTopicReport::getTopicId);
            Map<BigDecimal, ClassesTopic> classesTopicMap = classesTopicBiz.findClassesTopicByIds(topicIds, false, false, false);
            classesTopicReportList.forEach((v) -> v.setClassesTopic(classesTopicMap.get(v.getTopicId())));
        }
    }

    /**
     * 设置评论的学员id
     *
     * @param classesTopicReportList 班级话题举报
     */
    private void setClassesTopicReportCreator(List<ClassesTopicReport> classesTopicReportList) {
        if (ObjectUtils.isNotEmpty(classesTopicReportList)) {
            String cusUserIds = DataUtil.joining(classesTopicReportList, ClassesTopicReport::getUserId);
            Map<BigDecimal, Map<String, Object>> cusUserMap = cusUserService.queryCusUserMapByUserIds(cusUserIds, false, false, false, false, false, false, false);
            classesTopicReportList.forEach((v) -> v.setUser(cusUserMap.get(v.getUserId())));
        }
    }


    private String where(ClassesTopicReport condition) {
        String where = " status = 1";
        if (DataUtil.idIsNotNull(condition.getTopicId())) {
            where += " and topicId = " + condition.getTopicId();
        } else if (StringUtils.isNotEmpty(condition.getTopicIds())) {
            where += " and topicId in ( " + condition.getTopicIds() + ")";
        }
        if (DataUtil.idIsNotNull(condition.getUserId())) {
            where += " and userId = " + condition.getUserId();
        } else if (StringUtils.isNotEmpty(condition.getUserIds())) {
            where += " and userId in ( " + condition.getUserIds() + ")";
        }if (StringUtils.isNotEmpty(condition.getCreateTime())){
            where += " and createTime = " + condition.getCreateTime();
        }
        where += " order by createTime desc";
        return where;
    }
}
